/*
 * HubInfoInternacionalConnection.java
 *
 * Created on July 11, 2006, 8:01 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.conn;

import libjdc.dc.client.User;
import libjdc.dc.protocol.Encripter;
import libjdc.dc.protocol.Protocol;
import hubsniffer.*;
import libjdc.dc.log.Log;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.StringTokenizer;

/**
 *
 * @author root
 */
public class HubInfoInternacionalConnection extends HubInfoConnection{
    
    /** Creates a new instance of HubInfoInternacionalConnection */
    public HubInfoInternacionalConnection(DCHub hub,User me) {
        super(hub,me);
    }
    
    protected void processHubMessage(String message) throws IOException {
        try {
            Log.out.println(message);
            if (message.startsWith(Protocol.DC_S_MESSAGE_LOCK))
                handlerLock(message);
            else if (message.startsWith(Protocol.DC_S_MESSAGE_HUBINFO))
                super.handlerHubInfo(message);
            else if (message.startsWith(Protocol.DC_S_MESSAGE_NICKLIST))
                handlerNickList(message);
        }catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(Log.err);
        }
    }
    
    protected void handlerLock(String lock) throws ParseException, UnsupportedEncodingException, IOException{
        Object[] obj = Protocol.LOCK_PARSER.parse(lock);

        this.sendBotInfo(this.user.getUserInfo().getDescription(),new Encripter(obj[0].toString()).getKey());
        super.sendValidateNick();
        //sendVersion();
        super.sendGetNickList();
    }

    protected void handlerNickList(String nickList) {
        StringTokenizer users  = new StringTokenizer(nickList,""+Protocol.COMMAND_USER_SEP);
        String user;
        while(users.hasMoreTokens()){
            user = users.nextToken();
            if(!user.equals(""+Protocol.COMMAND_END_CHAR))
                super.getHub().getHubSession().getUserList().add(new User(user));
        }
    }
    
}
